package productidentification;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import common.Result;

/**
 * @author Paula Chen (Javadoc), Ryan Cheatham (implementation)
 * 
 *         Registry for product identifier.
 * 
 */
public class ProductIdentifierRegistry
{

  private List<ProductIDRegistryEntry> entries;

  /**
   * Constructs a new product identifier registry.
   */
  public ProductIdentifierRegistry()
  {
    entries = new ArrayList<ProductIDRegistryEntry>();
  }

  /**
   * register a product identifier registry entry
   * 
   * @param entry
   *          a product id registry entry to register
   * @return result indicating if the operation is done successfully
   */
  public Result register(ProductIDRegistryEntry entry)
  {
    if (entries.contains(entry))
      return new Result(false, "Cannot add entry - entry is already in the registry!");

    entries.add(entry);

    return new Result(true);
  }

  /**
   * unregister a product identifier registry entry
   * 
   * @param entry
   *          a product id registry entry to unregister
   * @return result indicating if the operation is done successfully
   */
  public Result unRegister(ProductIDRegistryEntry entry)
  {
    if (!entries.remove(entry))
      return new Result(false, "Could not remove entry - entry was not in the registry!");

    return new Result(true);
  }

  /**
   * get the product identifier registry entries
   * 
   * @return a iterator of the product identifier registry entries
   */
  public Iterator<ProductIDRegistryEntry> getEntries()
  {
    return Collections.unmodifiableList(entries).iterator();
  }
}
